package com.xoa.service.inv;

import com.xoa.dao.inv.ProductTypeMapper;
import com.xoa.model.inv.ProductType;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ProductTypeService {
    @Resource
    private ProductTypeMapper productTypeMapper;

    public ToJson<ProductType> insertProductType(HttpServletRequest request, ProductType productType) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            if(productType!=null){
                if(!StringUtils.checkNull(productType.getProductTypeName())&& productType.getProductTypeName()!="" ){
                    ProductType productType1 = productTypeMapper.selectProductTypeByName(productType.getProductTypeName());
                    if(productType1!=null){
                        toJson.setFlag(1);
                        toJson.setMsg("商品类别已存在，请重新填写");
                    }else {
                        int count=productTypeMapper.insertSelective(productType);
                        if(count>0){
                            toJson.setObject(productType);
                            toJson.setMsg("ok");
                            toJson.setFlag(0);
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<ProductType> updateProductType(HttpServletRequest request, ProductType productType) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            if(productType!=null){
                if(!StringUtils.checkNull(productType.getProductTypeName())&& productType.getProductTypeName()!="" ){
                    ProductType productType1 = productTypeMapper.selectProductTypeByName(productType.getProductTypeName());
                    if(productType1!=null){
                        toJson.setFlag(1);
                        toJson.setMsg("商品类别已存在，请重新填写");
                    }else {
                        int count=productTypeMapper.updateByPrimaryKeySelective(productType);
                        if(count>0){
                            toJson.setObject(productType);
                            toJson.setMsg("ok");
                            toJson.setFlag(0);
                        }
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<ProductType> selectProductTypeById(HttpServletRequest request, Integer productTypeId) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            ProductType productType=productTypeMapper.selectByPrimaryKey(productTypeId);
            if(productType!=null){
                toJson.setObject(productType);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<ProductType> deleteProductTypeById(HttpServletRequest request, Integer productTypeId) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            productTypeMapper.deleteByPrimaryKey(productTypeId);
            toJson.setMsg("ok");
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<ProductType> selectProductTypeInfo(HttpServletRequest request) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            List<ProductType> list=productTypeMapper.selectProductTypeInfo();
            if(list!=null&&list.size()>0){
                toJson.setObj(list);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    public ToJson<ProductType> selectProductTypeByName(HttpServletRequest request, String productTypeName) {
        ToJson<ProductType> toJson =new ToJson<ProductType>(1,"err");
        try {
            ProductType productType=productTypeMapper.selectProductTypeByName(productTypeName);
            if(productType!=null){
                toJson.setObject(productType);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setFlag(1);
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
